<template>
  <!-- 新闻中心 -->
  <div class="container">
    <van-tabs swipeable @change="switchTab" v-model="activeIndex">
      <van-tab v-for="(tab, i) in channels" :key="tab.id" :title="tab.name">
        <!-- 新闻列表 => 动态ref=》一一对应 -->
        <ArticleList :ref="'alist' + i" :channelId="tab.id" />
      </van-tab>
    </van-tabs>
    <!-- 右侧图标 -->
    <span class="bar_btn" @click="showEditChannel = true">
      <van-icon name="wap-nav"></van-icon>
    </span>
    <!-- 频道管理-弹层 -->
    <ChannelEdit
      :myChannels="channels"
      :activeIndex.sync="activeIndex"
      v-model="showEditChannel"
    />
  </div>
</template>

<script>
import { channels } from '../../api/user'
// 导入文章列表组件
import ArticleList from './articleList'
// 导入频道管理子组件
import ChannelEdit from './components/channel-edit'
export default {
  name: 'home-index',
  components: {
    ArticleList,
    ChannelEdit
  },
  // 被缓存组件再次被打开时执行
  activated () {
    console.log('被缓存组件再次被打开时执行')
    this.$refs['alist' + this.activeIndex] && this.$refs['alist' + this.activeIndex][0].scrollToList()
  },
  created () {
    console.log('home-index被创建了！', this.$parent)
    this.getChannels()
  },
  mounted () {
    console.log(
      'over---------------'
    )
  },
  // 处理refresh_token失效，回跳缓存不刷新问题
  watch: {
    '$store.state.user.token' () {
      console.log('refresh')
      this.getChannels()
    }
  },
  data () {
    return {
      // 我的频道数据
      channels: [],
      // 当前选中标签页的索引
      activeIndex: 0,
      // 控制频道管理面板显示
      showEditChannel: false
    }
  },
  methods: {
    // 获取频道数据
    async getChannels () {
      const { data: { channels: chan } } = await channels()
      this.channels = chan
    },
    // 切换标签页
    switchTab (i) {
      // this.activeIndex = i
      console.log('切换标签页：', i, this.$refs)
      // eslint-disable-next-line no-unused-expressions
      // this.$refs.alist[i]?.scrollToList()
      this.$refs['alist' + i] && this.$refs['alist' + i][0].scrollToList()
    }

  }
}
</script>

<style scoped lang='less'>
// 频道
.van-tabs {
  height: 100%;
  display: flex;
  flex-direction: column;
  /deep/ .van-tabs__wrap {
    height: 36px;
    padding-right: 36px;
    .van-tab {
      line-height: 36px;
    }
    .van-tabs__line {
      background-color: #3296fa;
      height: 2px;
    }
  }
  /deep/ .van-tabs__content {
    flex: 1;
    overflow: hidden;
  }
  /deep/ .van-tab__pane {
    height: 100%;
    .scroll-wrapper {
      height: 100%;
      overflow-y: auto;
    }
  }
}
.bar_btn {
  width: 36px;
  height: 35px;
  position: absolute;
  top: 0;
  right: 0;
  &::before {
    content: "";
    width: 100%;
    height: 100%;
    position: absolute;
    z-index: 999;
    box-shadow: 0 0 10px #999;
    transform: scale(1, 0.6);
  }
  .van-icon-wap-nav {
    width: 100%;
    height: 100%;
    background: #fff;
    text-align: center;
    line-height: 35px;
    position: relative;
    z-index: 1000;
    &::before {
      font-size: 20px;
    }
  }
}
</style>
